/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package NamNH;

import Entity.Account;
import Entity.Document;
import Entity.Event;
import Entity.Payment;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author dukynguyen
 */
@Stateless
public class NamNHSessionBean implements NamNHSessionBeanLocal {
    @PersistenceContext(unitName = "Group05_NationalLevelPaperPresentation-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    public boolean insertPayment(int EvID, String Username, String Price) {
        try {
            Event objEv = em.find(Event.class, EvID);
            Account objAcc = em.find(Account.class, Username);
            Payment objPay = new Payment();
            objPay.setEvent(objEv);
            objPay.setAccount(objAcc);
            objPay.setPrice(Price);
            objPay.setDateCreate(new Date());
            objPay.setIDelete(false);
            
            persist(objPay);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public UserPayDTO userViewHistoryPay(String Username) {
        try {
            String strQL = "SELECT a FROM Account a WHERE a.username = :username AND a.iDelete = :iDelete";
            Query query = em.createQuery(strQL);
            query.setParameter("username", Username);
            query.setParameter("iDelete", false);
            List userList = query.getResultList();
            UserPayDTO objDTO = new UserPayDTO();
            for (int i= 0; i < userList.size(); i++) {
                Account objAcc =(Account) userList.get(i);
                Collection AcColl = objAcc.getPaymentCollection();
                Payment[] ListPay = new Payment[AcColl.size()];
                AcColl.toArray(ListPay);

                objDTO.setObjAcc(objAcc);
                objDTO.setListPayment(ListPay);
            }
            return objDTO;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean UploadDocument(int EvId, String user) {
        boolean check = false;
        try {
            Document objD = new Document();
            Account objA = em.find(Account.class, user);
            Event objE = em.find(Event.class, EvId);
            objD.setEvent(objE);
            objD.setAccount(objA);
            objD.setDateCreate(new Date());
            objD.setIDelete(false);

            persist(objD);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public NamNH.AccountDocument historyDocument(String Username) {
        try {
            String strQL = "SELECT a FROM Account a WHERE a.username = :username AND a.iDelete = :iDelete";
            Query query = em.createQuery(strQL);
            query.setParameter("username", Username);
            query.setParameter("iDelete", false);
            List userList = query.getResultList();
            AccountDocument objDTO = new AccountDocument();
            for (int i= 0; i < userList.size(); i++) {
                Account objAcc =(Account) userList.get(i);
                Collection AcColl = objAcc.getDocumentCollection();
                Document[] ListPay = new Document[AcColl.size()];
                AcColl.toArray(ListPay);

                objDTO.setObjA(objAcc);
                objDTO.setListDoc(ListPay);
            }
            return objDTO;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean updateDocumment(int id, String url) {
        boolean check = false;
        try {
            Document objD = em.find(Document.class, id);
            objD.setDocURL(url);

            persist(objD);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();;
        }
        return check;
    }

    public Document findDocumentByPK(int id) {
        try {
            Document objD = em.find(Document.class, id);
            return objD;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean setMarkDocument(int id, String mark) {
        boolean check = false;
        try {
            Document objD = em.find(Document.class, id);
            objD.setDocMark(mark);

            persist(objD);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public boolean setWinner(int id, String winner) {
        boolean check = false;
        try {
            Event objE = em.find(Event.class, id);
            objE.setWinner(winner);

            persist(objE);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

 
}
